//
//  SearchTableViewCell.m
//  礼物go
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "KeyWordCollectionViewCell.h"


#define keyWordHeight 30
#define Space 10

@interface SearchTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UICollectionView *keyWordCollectionV;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation SearchTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.dataArray = [NSMutableArray array];
        [self createSubViews];
        [self getDataWord];
    }
    return self;
  }


-(void)createSubViews{
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.text = @"大家都在搜";

        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        flowLayOut.itemSize = CGSizeMake(80, keyWordHeight);
        flowLayOut.sectionInset = UIEdgeInsetsMake(Space * 2, Space, Space, Space * 2);
    
    self.keyWordCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), SCREENWIDTH, 200) collectionViewLayout:flowLayOut];
    self.keyWordCollectionV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.keyWordCollectionV];
    self.keyWordCollectionV.delegate = self;
    self.keyWordCollectionV.dataSource = self;
    [self.keyWordCollectionV registerClass:[KeyWordCollectionViewCell class] forCellWithReuseIdentifier:@"keyWordCollectionCell"];

}

-(void)layoutSubviews{
    [super layoutSubviews];
      self.titleLabel.frame = CGRectMake(0, 0, SCREENWIDTH, keyWordHeight);
    self.keyWordCollectionV.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), SCREENWIDTH, 200);
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KeyWordCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"keyWordCollectionCell" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor purpleColor];
    
    NSString *key = [self.dataArray objectAtIndex:indexPath.row];
    cell.keyWord = key;

    cell.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    cell.layer.borderWidth = 1;
    
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = [self.dataArray objectAtIndex:indexPath.row];
    self.keywordBlock(key);
    
}




-(void)getDataWord{
NSString *urlStr = @"http://api.liwushuo.com/v2/search/hot_words";
 [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
     NSDictionary *dataDic = [result objectForKey:@"data"];
     self.dataArray = [dataDic objectForKey:@"hot_words"];
     [self.keyWordCollectionV reloadData];
     
     
 } error:^(NSError *error) {
     
     
 }];


}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
